/**
 * @date 2017-9-11
 * @author wyl
 */
package com.nwcl.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpUtil {

    // 发送GET请求
    public static String doGet(String url, Map<String, String> parameters) throws Exception {
        StringBuilder urlString = new StringBuilder(url);
        if (parameters != null && !parameters.isEmpty()) {
            urlString.append("?");
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                urlString.append(entry.getKey())
                        .append("=")
                        .append(entry.getValue())
                        .append("&");
            }
            urlString.deleteCharAt(urlString.length() - 1); // 删除最后一个多余的"&"符号
        }

        URL requestUrl = new URL(urlString.toString());
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        connection.setRequestMethod("GET");

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            return response.toString();
        } else {
            throw new Exception("GET request failed. Response code: " + responseCode);
        }
    }

    // 发送POST请求
    public static String doPost(String url, Map<String, String> parameters) throws Exception {
        StringBuilder requestBody = new StringBuilder();
        if (parameters != null && !parameters.isEmpty()) {
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                requestBody.append(entry.getKey())
                        .append("=")
                        .append(entry.getValue())
                        .append("&");
            }
            requestBody.deleteCharAt(requestBody.length() - 1); // 删除最后一个多余的"&"符号
        }

        URL requestUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);

        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(requestBody.toString().getBytes());
        outputStream.flush();
        outputStream.close();

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            return response.toString();
        } else {
            throw new Exception("POST request failed. Response code: " + responseCode);
        }
    }
}
